package com.tjy.springboot.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tjy.springboot.beanVo.Result;
import com.tjy.springboot.mapper.UserMapper;
import com.tjy.springboot.model.Emp;
import com.tjy.springboot.beanVo.EmpVo;
import com.tjy.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public List<Emp> findAll() {
        return userMapper.selectAll();
    }

    @Override
    public PageInfo<Emp> findPage(EmpVo empVo, Integer page, Integer rows) {
        Example example = new Example(Emp.class);
        Example.Criteria and = example.and();
        if(empVo!=null){
            //如果姓名不为空 就模糊查询姓名
            if(empVo.getEname() !=null){
                and.andLike("ename", "%"+empVo.getEname()+"%");
            }
            //如果最小工资不为空就 大于等于做条件
            if(empVo.getBeginSalary() !=null){
                and.andGreaterThanOrEqualTo("esalary",empVo.getBeginSalary());
            }
            //如果最大工资不为空就 小于等于做条件
            if(empVo.getEndSalary() != null){
                and.andLessThanOrEqualTo("esalary",empVo.getEndSalary());
            }
        }

        PageHelper.startPage(page,rows);
        return new PageInfo<>(userMapper.selectByExample(example));
    }

    @Override
    public void saveAdd(Emp emp) {
        //如果有id 便是修改操作
        if(emp.getEid()!=null){
            userMapper.updateByPrimaryKey(emp);
            //没有id就是添加操作
        }else {
            userMapper.insert(emp);
        }
    }

    @Override
    public Result del(Integer eid) {
        //删除操作
       Boolean success = userMapper.deleteByPrimaryKey(eid)>0;
        return new Result(success,success ? "删除成功":"删除失败")  ;
    }


    @Override
    public Emp updateSelect(Integer eid) {
        return userMapper.selectByPrimaryKey(eid);
    }
}
